Okay.
In this lecture, we're going to continue scripting for our game by having the player's camera rotate
around the map when they are in the main menu, and then return the camera back to the player's character
when they spawn into the map.
We're going to create a dedicated local script for this task, and we're going to place it in starter
player scripts.
So we'll open up starter Player and in starter player scripts, we'll create a new local script.
And I'm going to call this my camera handler.
I'll use the same template as before.
We'll get rid of this section.
Get rid of that section.
And inside of here is where we're going to want to be able to manipulate our camera so we can go ahead
and make a reference to our camera.
I'll just call a variable camera and that's equal to the workspace.
And we can get our camera by referring to the current camera, which is the object being used by the
local player.
And then we also want to go ahead and grab that part in the workspace where we want to set our camera,
um, position to be equal to.
So that's going to be our camera part.
And that's going to be equal to workspace.
And inside of the filtering folder inside of the invisible folder we have our camera part.
And when our player first joins the game, we want to go ahead and set the player's camera to be positioned
at this camera part and then start rotating around the map.
In order to do that, we're going to need the run service.
So I'll get the run service.
And we can connect to an event in the run service, like the heartbeat event, which fires every frame
after the physics simulation to constantly rotate our player's camera around the map.
So what we could do is inside of this main section, we will.
Refer to our run service, get the heartbeat event, and we would connect a function to it that would
rotate our camera around the map.
So let's create a dedicated function for that.
And we're going to call it rotate camera.
And inside of here, all we're going to do is update the key frame of our camera to be equal to itself.
But we're going to add an angle using key frame dot angles.
And we want to rotate the camera around its y axis.
So we're going to do zero for radians on the x axis.
But then for radians on the y axis we're going to do math dot rad.
We're going to get the camera's original rotation.
So we're going to do camera dot key frame dot rotation and get its rotation on the y axis.
And then we want to go ahead and subtract this value by some kind of number.
And since this is going to be firing every single frame, we want this number to be quite small.
So we could do something like 0.002.
And then for the value on the Z we're going to do zero.
And while we are rotating the camera around the map, we also want to make sure we set the camera's
camera type mode, so specifies the camera type to be read by the camera scripts.
We want to make sure we set this equal to the enum dot camera type dot scriptable.
So it says no default behavior used when developers need to script custom behavior.
So we're scripting custom behavior for our camera.
We want it to rotate around the map.
So that's what we're going to be doing.
So we can connect rotate camera to the heartbeat event.
And then basically when we don't want the camera to be rotating around the map, we could disconnect
this function from our heartbeat event so it stops executing and then reset the player's camera back
to their character.
So that means we're going to have to store in a variable the connection return from our connect function.
So we can create a variable up here I'm going to call it um camera connection.
And we're not going to initialize it with any value.
But down here, we're going to get our camera connection and set it equal to what gets returned from
our connect function.
Because then in the future, we want to disconnect it.
We can simply refer to our camera connection and use the disconnect function it says disconnects the
connection from the event.
Now, since we have two different local script, one that's in our GUI, and then this one for manipulating
the camera, we want to be able to communicate between these two different scripts, so we can tell
this script when to start rotating the camera and when to stop rotating the camera.
And that's why inside of replicated storage and our remotes.
And then in our GUI folder I have a bindable event here called Rotate Camera.
And this allows local scripts to communicate with our camera handler when to start or stop rotating
the camera.
So we can go ahead and make a variable to refer to that event.
I will call it um, rotate camera event and that means we're going to have to get replicated storage.
So replicated storage is equal to game get service replicated storage.
And then we can set this equal to replicated storage dot remotes dot guy.
And then get the rotate camera Bindable event.
And then inside of our handler section we can go ahead and listen to our event.
So rotate camera event.
And then we can get the dot event it says fires when any script calls the fire method on the same bindable
event instance, we can connect a function to that.
And what we could do is we could expect any local script to pass us a boolean of whether or not we want
to start or stop rotating the camera.
So we'll create a parameter called bool, and this will be a boolean of true or false.
And if this boolean is true, then that means we should start rotating our camera around the map.
Otherwise we need to stop rotating our camera around the map.
So if we want to stop rotating our camera around the map, then we would get our camera connection and
we would disconnect it, which would stop the rotation.
And then we would set the camera's camera type to be equal to the enum dot camera type dot custom says
default mode used by Roblox core scripts, and by setting it back to the custom camera type, it's going
to move the camera back to our players character.
However, if we want to rotate it around the map, then what we're going to do is we're going to set
camera, dot camera, type back to the camera type of scriptable.
We want to set the camera C frame equal to our camera's part C frame.
So camera part dot C frame.
And then we want to go ahead and reconnect the rotate camera function back to the heartbeat event.
So we're going to update camera connection to be equal to run service dot heartbeat.
And we're going to connect rotate camera.
Now before we do this we want to make sure that there isn't any previous connections stored in this
variable.
And if there is we want to go ahead and disconnect it.
So if we have a value stored or if we have a connection and our camera connection variable, then we
want to make sure we disconnect it.
So we'll do camera connection disconnect.
So that way we're okay to override the value in here with a new connection that's connected to our heartbeat
event.
And then since this script is going to execute when our player joins the game and we want the player's
camera to start rotating around the map when they join the game, down here in our main section, we
want to make sure we update the position or C frame of our camera to be equal to that position on our
map, so we can get our camera, and we'll make sure we update the C frame of our camera equal to the
camera part.
Dot cframe.
Now, before we do this, we also want to make sure that we update the camera type of our camera equal
to the enum dot camera type of scriptable.
That way we are able to update and move the keyframe of our camera to the correct position.
So now that means when our player joins the game and their camera's rotating around the map, we want
to stop the player's camera from rotating around the map when they press the play button and they spawn
into the game.
And then we also want to start rotating their camera back around the map when the player dies, and
then the GUI or the main menu comes back on their screen.
So back inside of our starting menu handler, what we can go ahead and do is get that rotate camera
event to communicate with our camera handler.
So we can call this rotate camera event.
And that's in replicated storage dot remotes, dot GUI dot rotate camera.
And then when we activate our play button, after we tell the server to respawn us, what we could do
is get our rotate camera event and fire and let our camera handler know that, hey, we want to stop
rotating the camera around the map so we we pass false to that event.
However, when our player dies, right, we want to start rotating the camera back around the map again.
So after we have waited for a character to be added, we can use our rotate, camera event and fire.
And this time we could say, hey, please start rotating the camera back around the map again.
So now if we go and playtest our game, our camera should be rotating around the map, and then we should
be able to press the play button spawn in, and our camera should stop rotating.
So if we go ahead and hit play.
Looks like we got an error.
Camera part is not a valid member of the filtering folder and in this folder.
So let's go ahead and see what we did wrong.
If we go and look in the workspace um camera part is a member of these folders.
Oh I know what's wrong.
If we look in the workspace.
The problem is, is that we have streaming enabled.
So streaming enabled is enabled by default for all new games you create.
And since this is a small game we have I'm going to go ahead and disable it.
But because streaming enabled was enabled um, all of these parts were replicated to our player over
time.
And that means when these scripts executed, our camera part was not immediately available to us in
the workspace.
But since we've disabled streaming now, when the player joins the game, when these scripts execute,
everything in the workspace should be replicated to us, and that means it's okay for us to go ahead
and directly index for our camera part.
If we were using streaming enabled, then that means we would have to use the wait for child event to
go ahead and wait for these different instances to replicate to our client.
But again, since this is going to be a small map and a small game, there's no need for us to have
streaming enabled.
So now if we go and play test, our problem should be fixed.
Okay, the error is gone and there we go.
Our camera is now rotating around the map.
And then if I go and play test the game.
Our camera should have reset.
Perfect.
Our camera is back on our player.
And then if my player were to die.
So if I reset, our camera should be back rotating around the map again.
Just like that.
Perfect.
So just like that, we've added camera rotation to our game.
I'll see you in the next lecture.